package com.ygsoft.activiti.service.impl;

import com.ygsoft.activiti.service.RuntimeServiceInf;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author zhx
 * @create 2017-11-13 17:09
 **/
@Component
public class RuntimeServiceImpl implements RuntimeServiceInf {

    @Autowired
    private IdentityService identityService;

    @Autowired
    private RuntimeService runtimeService;

    @Override
    public boolean startProcessById(String userId, String processId) {
        try {
            //设置流程定义中的applyUserId的值
            identityService.setAuthenticatedUserId(userId);
            //发起流程实例
            runtimeService.startProcessInstanceById(processId);

            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public String startProcessByKey(String userId, String processKey) {
        try {
            //设置流程定义中的applyUserId的值
            identityService.setAuthenticatedUserId(userId);
            //发起流程实例
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey);
            //返回当前申请的流程id
            String instanceid=processInstance.getId();
            return instanceid;
        }catch (Exception e){
            return "";
        }
    }

    @Override
    public String startProcessByKey(String userId, String processKey, String bussinessId, Map<String, Object> variables) {
        //开始之前的检查  TODO
        //设置流程定义中的applyUserId的值
        identityService.setAuthenticatedUserId(userId);
        //发起流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, bussinessId, variables);
        //返回当前申请的流程id
        String instanceid=processInstance.getId();
        return instanceid;
    }
}
